<template>
  <div class="app-container">
    <div class="filter-container">
      <el-select v-model="listQuery.timeType" placeholder="类型" class="filter-item">
        <el-option v-for="item in timeJson" :key="item.value" :label="item.label" :value="item.value" />
      </el-select>
      <div class="filter-item">
        <AddressCascader :isShowProvinceAndCity="true" style="width: 200px" @selectAddressDetail="handleAddress" placeholder="选择省市"/>
      </div>
      <div class="filter-item">
        <div class="searchItem">
          <span class="searchLabel">筛选日期：</span>
          <DatePicker :mode="1" :startDate.sync="listQuery.beginTime" :endDate.sync="listQuery.endTime" clearable />
        </div>
      </div>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="getData">
        查询
      </el-button>

      <div ref="lineChartData" class="line"></div>
    </div>
  </div>
</template>

<script>

import * as echarts from 'echarts/core';
import { TooltipComponent, GridComponent, TitleComponent, LegendComponent } from 'echarts/components';
import { LineChart } from 'echarts/charts';
import { UniversalTransition } from 'echarts/features';
import { CanvasRenderer } from 'echarts/renderers';

echarts.use([
  TooltipComponent,
  GridComponent,
  LineChart,
  CanvasRenderer,
  TitleComponent,
  LegendComponent,
  UniversalTransition
]);

import { queryOrderDataAnalysis } from '@/api/zcyx'
import DatePicker from '@/components/DatePicker'
import waves from '@/directive/waves'
import AddressCascader from '@/components/AddressCascader'

export default {
  name: "OrderAnalysis",
  directives: { waves },
  props: {

  },
  components: {
    DatePicker,
    AddressCascader
  },
  data() {
    return {
      listQuery: {
        type: '1',
        timeType: '3',
        beginTime: '',
        endTime: '',
      },
      timeJson: [
        { value: '1', label: '年' },
        { value: '2', label: '月' },
        { value: '3', label: '日' },
      ],
      address: {},
      option: {
        // color: "white",
        title: {
          text: '平台',
          left: 70
        },
        tooltip: {
          trigger: 'axis'
        },
        legend: {
          data: ['订单数量', '总金额', '客单价']
        },
        label: {
          show: true,
          color: 'white',
          fontStyle: 'normal',
          fontSize: 14
        },
        xAxis: {
          type: 'category',
          // boundaryGap: false, // 去除轴间距
          // 去除刻度线
          axisTick: {
            // show: false
          },
          axisLine: {
            show: true // 去除轴线
          },
          // axisLabel: {
          //   color: "white",
          //   fontSize: 13,
          //   // formatter: function (value) {
          //   //   var strList = value.split("-")
          //   //   return strList[1]  + "月"
          //   // }
          // }
        },
        yAxis: {
          type: 'value',
          axisLabel: {
            color: "white",
          },
          axisTick: {
            show: false
          },
          axisLine: {
            show: false // 去除轴线
          },
          // splitLine:{
          //   show:false
          // },
          axisLabel: {
            // color: "white",
            // fontSize: 14,
          }
        },
        animation: true,
        series: [
          {
            name: '订单数量',
            data: [],
            type: 'line',
            smooth: true,
            label: {
              show: true
            },
          },
          {
            name: '总金额',
            data: [],
            type: 'line',
            smooth: true,
            label: {
              show: true
            },
          },
          {
            name: '客单价',
            data: [],
            type: 'line',
            smooth: true,
            label: {
              show: true
            },
          }
        ]
      }
    }
  },
  computed: {

  },
  watch: {},
  methods: {
    handleAddress(val) {
      this.address = val
    },
    getData() {
      this.getList(1)
      this.getList(2)
      this.getList(3)
    },
    getList(type) {
      let param = Object.assign({}, this.listQuery)
      param.type = type
      if (this.address && this.address.provinceId) {
        param.receiveProvinceId = this.address.provinceId
        param.receiveCityId = this.address.cityId
      }
      queryOrderDataAnalysis(param).then(response => {
        var list = (response || {})
        if (this.address && this.address.provinceId) {
          this.option.title.text = this.address.provinceName + this.address.cityName
        } else {
          this.option.title.text = "平台"
        }
        this.handleList(type, list)
      }).catch(error => {
        console.log(error);
      })
    },
    handleList(type, list) {
      if (list && list.length > 0) {
        var valueList = []
        var xAxisList = []
        list.forEach((item) => {
          if (type == 1) {
            valueList.push(item.num)
          } else if (type == 2) {
            valueList.push(item.amount)
          } else {
            valueList.push(item.customerAmount)
          } 
          
          xAxisList.push(item.createTime)
        })

        if (type == 1) {
          this.option.series[0].data = valueList
        } else if (type == 2) {
          this.option.series[1].data = valueList
        } else {
          this.option.series[2].data = valueList
        } 
        
        this.option.xAxis.data = xAxisList 
        
        var chartDom = this.$refs.lineChartData;
        var myChart = echarts.init(chartDom);
        myChart.setOption(this.option)
      }
    }
  },
  created() {
    this.getData()
  },
  mounted() {
    
  },
  beforeCreate() {}, // 生命周期 - 创建之前
  beforeMount() {}, // 生命周期 - 挂载之前
  beforeUpdate() {}, // 生命周期 - 更新之前
  updated() {}, // 生命周期 - 更新之后
  beforeDestroy() {}, // 生命周期 - 销毁之前
  destroyed() {}, // 生命周期 - 销毁完成
  activated() {}, // 如果页面有keep-alive缓存功能，这个函数会触发
}
</script>

<style lang='scss' scoped>
  .searchItem {
    display: flex;
    align-items: center;
    .searchLabel {
      color: #666;
    }
  }

  .line {
    position: relative;
    width: 100%;
    height: 500px;
  }
</style>